package com.usefullc.crawler.utils;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;
import java.io.InputStream;

public class AlarmSoundPlayer {

    public static void main(String[] args) {
        playAlarmSound();
    }

    public static void playAlarmSound() {
        try {
            // 加载音频文件
            // 使用 getResourceAsStream() 获取资源文件的输入流
            String resourcePath = "warn.mp3"; // 将路径替换为实际的资源文件路径
            InputStream inputStream = ClassLoader.getSystemResourceAsStream(resourcePath);
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputStream);
            // 获取音频剪辑
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            // 播放音频
            clip.start();
            // 等待音频播放完成
            Thread.sleep(clip.getMicrosecondLength() / 1000);
            // 关闭音频剪辑
            clip.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
